//
//  ViewController.m
//  AppIAPTest
//
//  Created by 我叫咚咚枪 on 2017/9/4.
//  Copyright © 2017年 DDQ. All rights reserved.
//

#import "ViewController.h"

#import "DDQIAPHelper.h"
#import <StoreKit/StoreKit.h>
#import <MBProgressHUD/MBProgressHUD.h>
#import <AVFoundation/AVFoundation.h>

@interface ViewController ()

@property (nonatomic, copy) DDQIAPHelper *hepler;
@property (nonatomic, strong) MBProgressHUD *hud;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.hepler = [DDQIAPHelper helper];
    self.hud = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:self.hud];
    self.hud.label.text = @"等会着。。。。";

//    UIScreenMode *mode = [UIScreen mainScreen].currentMode;
//    NSLog(@"%@, %f", NSStringFromCGSize(mode.size), mode.pixelAspectRatio);
//    [mode addObserver:self forKeyPath:@"pixelAspectRatio" options:NSKeyValueObservingOptionNew context:nil];
}

//- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
//
//    NSLog(@"%@", change);
//}

/**
 买星币
 */
- (IBAction)controller_buyStarCoin:(UIButton *)sender {

    if ([DDQIAPHelper iap_allow]) {
        
        NSString *identifier = nil;
        if (sender.tag == 1) {
            identifier = @"com.qx.xb42";
        } else if (sender.tag == 2) {
        
            identifier = @"com.qx.xb210";
        } else {
        
            identifier = @"com.qx.yearMember";
        }
        
        __weak typeof(self) weakSelf = self;
        [self.hud showAnimated:YES];
        [self.hepler iap_sendRequestWithProductIdentifier:identifier syncCompletion:^(NSError * _Nullable error) {
            
            [weakSelf.hud hideAnimated:YES];
            NSString *message = (error) ? error.domain:@"支付成功";
            UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                
                [weakSelf dismissViewControllerAnimated:YES completion:nil];
            }];
            [alertC addAction:action];
            
            [weakSelf presentViewController:alertC animated:YES completion:nil];

        } syncStatus:^(DDQIAPHeplerStatus status) {

            if (status == DDQIAPHeplerStatusEnd) {
                [weakSelf.hud hideAnimated:YES];
            } else {
                [weakSelf.hud showAnimated:YES];
            }
        }];
    }
}

@end
